package com.ruoyi.equipment.controller;

import com.hz.rouyigonggong.domain.basedomain.BaseDomain;
import com.hz.rouyigonggong.utils.ResultJSON;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.equipment.domain.LgTemperature;
import com.ruoyi.equipment.domain.LgVehicle;
import com.ruoyi.equipment.redis.RedisUtil;
import com.ruoyi.equipment.service.LgTemperatureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruoyi.common.core.utils.PageUtils.startPage;

/**
 * @Author: lek
 * @Description: TODO
 * @Date: 2025/3/5 11:45
 * @Version: 1.0
 */
@RestController
@RequestMapping("/temperature")
public class LgTemperatureController extends BaseController {
    @Autowired
    private LgTemperatureService lgTemperatureService;
    @Autowired
    RedisUtil redisUtil;

    /**
     * @author maisan
     * @description 查询所有设备信息
     * @createTime  2025/3/5 11:46
     * @param lgTemperature
     * @return
     **/
//    @RequiresPermissions("equipment:temperature:list")
    @GetMapping("/list")
    public TableDataInfo list(LgTemperature lgTemperature){
        startPage();
        lgTemperature.setCreateBy(String.valueOf(BaseDomain.getUserId()));
        System.out.println(BaseDomain.getUserId());
        List<LgTemperature> list = lgTemperatureService. selectLgTemperatureList(lgTemperature);
        return getDataTable(list);
    }
    /**
     * @author maisan
     * @description 导出
     * @createTime  2025/3/7 10:55
     * @return void
     **/

    @Log(title = "GPS设备管理", businessType = BusinessType.EXPORT)
//    @RequiresPermissions("equipment:temperature:export")
    @RequiresPermissions("equipment:temperature:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, LgTemperature lgTemperature)
    {
        List<LgTemperature> list = lgTemperatureService.selectLgTemperatureList(lgTemperature);
        ExcelUtil<LgTemperature> util = new ExcelUtil<LgTemperature>(LgTemperature.class);
        util.exportExcel(response, list, "GPS设备数据");
    }
    /**
     * @author maisan
     * @description 根据id查询设备信息
     * @createTime  2025/3/5 11:46
     * @param id
     * @return
     **/
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id){
        LgTemperature lgTemperature = lgTemperatureService.selectLgTemperatureById(id);
        if (lgTemperature == null){
            return AjaxResult.error("设备不存在");
        }
        return AjaxResult.success(lgTemperature);
    }

    /**
     * @author maisan
     * @description 修改设备信息
     * @createTime  2025/3/6 9:48
     * @param
     * @return
     **/

    @PutMapping
    public AjaxResult update(@Validated @RequestBody LgTemperature lgTemperature){
        int i = lgTemperatureService.updateLgTemperature(lgTemperature);
        if (i == 0){
            return AjaxResult.error("修改失败");
        }
        return AjaxResult.success();
    }

    /**
     * @author maisan
     * @description 新增设备信息
     * @createTime  2025/3/6 9:48
     * @param
     * @return
     **/

    @RequiresPermissions("equipment:temperature:add")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody LgTemperature lgTemperature){
        System.out.println("ssssssssss"+ BaseDomain.getUserId());
        lgTemperature.setCreateBy(String.valueOf(BaseDomain.getUserId()));
        int i = lgTemperatureService.insertLgTemperature(lgTemperature);
        if (i == 0){
            return AjaxResult.error("新增失败");
        }
        return AjaxResult.success();
    }

    /**
     * @author maisan
     * @description 根据id或ids删除设备信息
     * @createTime  2025/3/6 10:04
     * @param
     * @return
     **/
    @RequiresPermissions("equipment:temperature:remove")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids){
        int i = lgTemperatureService.deleteLgTemperatureByIds(ids);
        if (i == 0){
            return AjaxResult.error("删除失败");
        }
        return AjaxResult.success();
    }

    @RequiresPermissions("equipment:temperature:updateVehicle")
    @PutMapping("/updateVehicle")
    public AjaxResult updateVehicle(@RequestBody Map<String, Object> map){
        LgVehicle lgVehicle = lgTemperatureService.selectVehicleById((Integer) map.get("vehicleId"));
        LgTemperature lgTemperature = new LgTemperature();
        lgTemperature.setLgVehicle(lgVehicle);
        List<LgTemperature> lgTemperatureList = lgTemperatureService.selectLgTemperatureList(lgTemperature);
        if (lgTemperatureList.size() == 0){
            lgTemperature.setId((Integer) map.get("tid"));
            lgTemperature.setBoundId((Integer) map.get("vehicleId"));
            return AjaxResult.success(lgTemperatureService.updateLgTemperature(lgTemperature));
        }
        return AjaxResult.error("该设备已绑定");
    }
    /**
     * @author maisan
     * @description
     * @createTime  2025/3/6 19:55
     * @param [java.util.Map<java.lang.String,java.lang.Object>]
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     **/
    @RequiresPermissions("equipment:temperature:delBound")
    @PutMapping("/delBound")
    public AjaxResult delBound(@RequestBody Map<String, Object> map){
        LgTemperature lgTemperature = lgTemperatureService.selectLgTemperatureById((Integer) map.get("id"));
        lgTemperature.setBoundId(0);
        return AjaxResult.success(lgTemperatureService.updateLgTemperature(lgTemperature));
    }
    /**
     * @author maisan
     * @description 查询设备车辆信息
     * @createTime  2025/3/6 19:54
     * @param [java.util.Map<java.lang.String,java.lang.Object>]
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     **/
    @RequiresPermissions("equipment:temperature:select")
    @GetMapping("/select")
    public TableDataInfo selectLgVehicleList(LgVehicle lgVehicle){
        startPage();
        List<LgVehicle> list = lgTemperatureService.selectLgVehicleList(lgVehicle);
        return getDataTable(list);
    }

    //根据出库订单编号 查GPS设备信息
    @GetMapping("/getLocation/{stockOrderNumber}")
    public ResultJSON getLocation(@PathVariable Integer stockOrderNumber){
        return ResultJSON.success(lgTemperatureService.getTemperatureByOrder(stockOrderNumber));
    }

    //根据出库订单编号 查GPS设备信息1
    @GetMapping("/getLocationGo/{stockOrderNumber}")
    public ResultJSON getLocationGo(@PathVariable Integer stockOrderNumber){
        return ResultJSON.success(lgTemperatureService.getTemperatureByOrderGo(stockOrderNumber));
    }

    @GetMapping("/TemPic")
    public AjaxResult tempic(Integer id){


        Map<String,List> map  = new HashMap<>();

        map.put("temperature",redisUtil.getList("equipment:temperature:temperature"+ id));
        map.put("humidity",redisUtil.getList("equipment:temperature:humidity" + id));
        map.put("time",redisUtil.getList("equipment:temperature:time" + id));
       map.put("gps_x",redisUtil.getList("equipment:temperature:gps_x" + id));
       map.put("gps_y",redisUtil.getList("equipment:temperature:gps_y" + id));
        return AjaxResult.success(map);
    }
}
